import {createRouter, createWebHistory} from 'vue-router'
import {ElNotification} from "element-plus";
import SessionStorage from "@/SessionStorage.js";

const routes = [

    {
        path: '/',
        name: 'ToLogin',
        redirect: '/login',
        component: () => import('../views/Login.vue')
    },

    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/Register.vue')
    },
    // 前台页面路由
    {
        path: '/home',
        name: 'Home',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/homePage/Home.vue')
    },
    {
        path: '/movieDetail/:mid',
        name: 'MovieDetail',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/homePage/MovieDetailComment.vue')
    },
    {
        path: '/userInfo',
        name: 'UserInfo',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/homePage/UserInfo.vue')
    },
    {
        path: '/userOrders',
        name: 'UserOrders',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/homePage/UserOrders.vue')
    },
    {
        path: '/userComments',
        name: 'UserComments',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/homePage/UserComments.vue')
    },
    {
        path: '/selectOrganize/:mid',
        name: 'SelectOrganize',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/SelectOrganize.vue')
    },
    // 先选座，然后购票
    {
        path: '/buyTicket/:soId',
        name: 'BuyTicket',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/BuyTicket.vue')
    },
    {
        path: '/buySuccess',
        name: 'ButSuccess',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/frontStage/BuySuccess.vue')
    },
    {
        path: '/404',
        name: '404',
        component: () => import('../views/404.vue')
    },
    {
        path: '/test',
        name: 'test',
        component: () => import('../views/Test.vue')
    },
    {
        // 后台页面路由
        path: '/back',
        redirect: "/back/frontPage",
        component: () => import('../views/backStage/Manager.vue'),
        children: [//manager内的子路由
            {path: '/back/frontPage', name: '首页', component: () => import('../views/backStage/FrontPage.vue')},
            {path: '/back/user', name: '用户管理', component: () => import('../views/backStage/UserManage.vue')},
            {path: '/back/movie', name: '电影管理', component: () => import('../views/backStage/MovieManage.vue')},//meta可以定义另外的属性
            {path: '/back/cinema', name: '电影院管理', component: () => import('../views/backStage/CinemaManage.vue')},
            {path: '/back/hall', name: '影厅管理', component: () => import('../views/backStage/HallManage.vue')},
            {path: '/back/actor', name: '演员管理', component: () => import('../views/backStage/ActorManage.vue')},
            {path: '/back/movieType', name: '电影类型管理', component: () => import('../views/backStage/MovieTypeManage.vue')},
            {path: '/back/movieComment', name: '评论管理', component: () => import('../views/backStage/MovieCommentManage.vue')},
            {path: '/back/screeningOrganize', name: '场次管理', component: () => import('../views/backStage/ScreeningOrganizeManage.vue')},
        ]//子路由结束
    },
    {
        path: '/admin/movie',
        name: 'MovieManage',
        meta: {
            requireAuth: true
        },
        component: () => import('../views/backStage/MovieManage.vue')
    },
]
const router = createRouter({
    history: createWebHistory("/"),
    routes: routes
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // 记录当前路由
    // console.log(from);
    // localStorage.setItem("currentPathName",to.name);

    if (to.path === '/login') {
        return next();
    } else if (to.path === '/register') {
        return next();
    } else if (to.path === '/test') {
        return next();
    }
    // 获取用户信息并校验，失败则退回到登录注册界面
    const loginUser = SessionStorage.getItem("loginUser");
    if (!loginUser) {
        ElNotification({
            type: 'error',
            message: '获取用户信息异常，返回登录界面！'
        })
        return next('/login')
    }
    // 只有admin才能登录后台
    if (to.path.includes("/back") && loginUser.uRole !== '0') {
        ElNotification({
            type: 'error',
            message: '非法访问！'
        })
        return next('/home')
    }
    // 正常获取用户信息则放行
    next();
})
// 路由错误回调
router.onError((error) => {
    if (error.message.includes('Failed to fetch dynamically imported module')) {
        ElNotification({
            message: '系统已升级，正在刷新本地存储，请稍候...',
            type: 'warning',
            duration: 1500,
            offset: 60
        })
    }
})

// 输出对象
export default router;
